package com.stone.serverless.beetl.parase;

import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.stone.serverless.orm.annotation.TemplateSql;
import org.beetl.sql.mapper.MapperInvoke;
import org.beetl.sql.mapper.annotation.Sql;
import org.beetl.sql.mapper.builder.MapperMethodParser;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * @Description: TODO
 * @Author: MECHREVO
 * @Date: 2022/4/7 13:49
 * @Version: 1.0.0
 */
public class SeMapperMethodParser extends MapperMethodParser {
    /**
     * 映射方法解析器
     *
     * @param defaultRetType 接口实体类
     * @param mapperClass    BaseMapper接口或子接口
     * @param method         调用的接口方法
     */
    public SeMapperMethodParser(Class defaultRetType, Class mapperClass, Method method) {
        super(defaultRetType, mapperClass, method);
    }


    @Override
    public MapperInvoke parse() {

        /*直接提供jdbc的PrepareStatement语句*/
        Annotation[] annotations = method.getAnnotations();
        String sql = CharSequenceUtil.EMPTY;
        for (Annotation annotation : annotations) {
            if(annotation.annotationType().getName().equals(TemplateSql.class.getName())){
                sql = AnnotationUtil.getAnnotationValue(method,annotation.annotationType());
            }
        }
        if (CharSequenceUtil.isNotBlank(sql)) {
            Cc cc = new Cc(sql);
            MapperInvoke invoke = parseSqlMethod(cc);
            return invoke;
        }
       return super.parse();
    }

    public class Cc implements Sql {

        private String value = null;
        public Cc(String value){
            this.value =value;
        }
        @Override
        public boolean equals(Object obj) {
            return false;
        }

        @Override
        public int hashCode() {
            return 0;
        }

        @Override
        public String toString() {
            return null;
        }

        @Override
        public Class<? extends Annotation> annotationType() {
            return Sql.class;
        }

        @Override
        public String value() {
            return this.value;
        }
    }
}
